<?php
class TicketController {
	public function generate(){
		$prodcut_type_id_list = array(PRODUCT_TYPE_3,PRODUCT_TYPE_4,PRODUCT_TYPE_5,PRODUCT_TYPE_6,PRODUCT_TYPE_7);
		$product = HttpDaoManager::getProductDao()->getDataPairListByType($prodcut_type_id_list);
		$tmp = array();
		foreach ($product  as $v){
			$tmp[$v['id']] = $v['name'];
		}
		$data['productOption'] = page_modifier_options($tmp);
		
		return $data;
	}
	public function subGenerate(){
		$tmp = HttpServerManager::getTicketServer()->generate($_POST);
		if ($tmp===true){
			$msg  = Common::_t('data_generate_success');
			ClassManager::getToos()->msgbox($msg);
		}
		else{
			$msg = Common::_t('eror_msgbox');
			ClassManager::getToos()->msgbox($msg[$tmp]);
		}
	}
	
	public function export(){
		$data = $_POST;
		$export =HttpServerManager::getTicketServer()->pagingBySearch($_POST,"",10000);
		$export_data = array();
		if(!empty($export)){
			$export_data = HttpServerManager::getTicketServer()->exportData($export);
		}

		$file_name = date('Y-m-d').'_ticket_list.xls';
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Disposition: attachment;filename=" . $file_name . "");
		
		$strBegin = "<meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"UTF-8\">";
		$strContent = "<table  border=\"0\" cellpadding =\"1\" cellspacing =\"1\" border =\"1\"  width=\"100%\">\r\n";
		$strContent .= "<tr height=\"50\" bgcolor=\"#999999\"><td width=\"10%\">号码券</td>\r\n" ;
		$strContent .= "<td width=\"50%\">密码</td>" ;
	    $strContent .= "</tr>\r\n";
		$old_orderno="";
		$new_orderno="";
		for ($i = 0; $i < count($export_data); $i++) {
			$number=$export_data[$i]['number'];
			$raw_password = '';
			if(!empty($export_data[$i]['raw_password'])){
				$raw_password = $export_data[$i]['raw_password'];
			}
			
			$strContent .= "<tr height=\"15\"><td width=\"50%\"  style=\"vnd.ms-excel.numberformat:@\">" . $number . "</td>\r\n" ;
	        $strContent .= "<td width=\"50%\" align=\"left\"  style=\"vnd.ms-excel.numberformat:@\"> " . $raw_password . "</td></tr>\r\n";
		}
	
		$strEnd = "</table>\r\n";
	
		echo $strBegin . $strContent . $strEnd;
	}
	
	public function lists (){
		$data =  HttpServerManager::getTicketServer()->pagingBySearch($_GET);
		$ticket_id_list = array();
		$ticket_log_info=array();
		$product_info=array();
		$user_info=array();
		if(!empty($data['list'])){
			$ticket_id_list = Common::array_value_list($data['list'],'id');
			$product_id_list =  Common::array_value_list($data['list'],'product_id');
			$user_id_list=  Common::array_value_list($data['list'],'receive_user_id');
			$ticket_log_list = HttpDaoManager::getTicketLogDao()->getDataPairListByTicketIdList($ticket_id_list);
			foreach($ticket_log_list as $k=>$v){
				$ticket_log_info[$v['ticket_id']] = $v;
			}
			$product_list = HttpDaoManager::getProductDao()->getDataPairList($product_id_list);
			foreach($product_list as $k=>$v){
				$product_info[$v['id']] = $v;
			}
			$user_list = HttpDaoManager::getUserDao()->getDataPairList($user_id_list);
			foreach($user_list as $k=>$v){
				$user_info[$v['id']] = $v;
			}
		}
		
		$status =Common::_t('ticket_status');
		foreach ($data['list'] as $k=>$v ){
			$data['list'][$k]['status'] = $status[$data['list'][$k]['status']];
			$data['list'][$k]['use_user'] = $ticket_log_info[$v['id']]['use_user'];
			$data['list'][$k]['use_time'] = $ticket_log_info[$v['id']]['use_time'];
			$data['list'][$k]['use_user_id'] = $ticket_log_info[$v['id']]['use_user_id'];
			$data['list'][$k]['send_time'] = $ticket_log_info[$v['id']]['send_time'];
			$data['list'][$k]['send_admin'] = $ticket_log_info[$v['id']]['send_admin'];
			$data['list'][$k]['activation_time'] = $ticket_log_info[$v['id']]['activation_time'];
			$data['list'][$k]['activation_admin'] = $ticket_log_info[$v['id']]['activation_admin'];
			$data['list'][$k]['order_id'] = $ticket_log_info[$v['id']]['order_id'];
			$data['list'][$k]['product_name'] = $product_info[$v['product_id']]['name'];
			if($v['receive_user_id']>0){
				$data['list'][$k]['receive_email'] = $user_info[$v['receive_user_id']]['email'];
			}
		}
		
		$prodcut_type_id_list = array(PRODUCT_TYPE_3,PRODUCT_TYPE_4,PRODUCT_TYPE_5,PRODUCT_TYPE_6);
		$product = HttpDaoManager::getProductDao()->getDataPairListByType($prodcut_type_id_list);
		$tmp = array();
		foreach ($product  as $v){
			$tmp[$v['id']] = $v['name'];
		}

		$data['productOption'] = page_modifier_options($tmp,Common::getGetStrValue('product_id',""));
		$data['statusOption'] = page_modifier_options($status,Common::getGetStrValue('status',""));

		$ticket_used_type_options = Common::_t('ticket_used_type');
		$ticket_send_type_options = Common::_t('ticket_send_type');

		$data['ticketSendTypeOption'] = page_modifier_options($ticket_send_type_options,Common::getGetStrValue('ticket_send_type',""));
		$data['ticketUsedTypeOption'] = page_modifier_options($ticket_used_type_options,Common::getGetStrValue('ticket_used_type',""));
		return $data;
	}
	public function activation(){
		HttpServerManager::getTicketServer()->activation($_GET['id']);
		$msg = Common::_t('data_activation_success');
		ClassManager::getToos()->msgbox($msg);
	}
	public function cancellation(){
		HttpServerManager::getTicketServer()->cancellation($_GET['id']);
		$msg = Common::_t('data_cancellation_success');
		ClassManager::getToos()->msgbox($msg);
	}
	public function send(){
		//只有代金券发放
		$prodcut_type_id_list = array(PRODUCT_TYPE_3);
		$product = HttpDaoManager::getProductDao()->getDataPairListByType($prodcut_type_id_list);
		$tmp =array();
		foreach ($product as $v){
			$tmp[$v['id']] = $v['name'];
		}
		$data['productOption'] =  page_modifier_options($tmp);
		$user_send_type_options = Common::_t('user_send_type');
		$data['user_send_type_options'] =  page_modifier_options($user_send_type_options);
		
		return $data;
	}
	public function subSend(){
		if(empty($_POST['user']['email'])&&empty($_POST['user']['phone'])&&empty($_POST['user']['start_time'])&&empty($_POST['user']['over_time'])){
			ClassManager::getToos()->msgbox('请选择相应的发送条件');
		}
		$tmp = HttpServerManager::getTicketServer()->batchSend($_POST['user'], $_POST['ticket']);
		if ($tmp===true){
			$msg = Common::_t('data_send_success');
			ClassManager::getToos()->msgbox($msg);
		}
		else{
			$msg = Common::_t('eror_msgbox');
			ClassManager::getToos()->msgbox($msg[$tmp]);
		}
	}
	public function exchange(){
		
	}

}
?>